package org.qianye.soft.ext.grid;

import java.awt.BorderLayout;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.HighlighterFactory;
import org.qianye.soft.ext.JLoadComponent;

import com.google.gwt.event.HandlerRegistration;
import com.sencha.gxt.data.shared.loader.BeforeLoadEvent;
import com.sencha.gxt.data.shared.loader.ListLoadConfig;
import com.sencha.gxt.data.shared.loader.ListLoadResult;
import com.sencha.gxt.data.shared.loader.ListLoader;
import com.sencha.gxt.data.shared.loader.LoadEvent;
import com.sencha.gxt.data.shared.loader.LoadExceptionEvent;
import com.sencha.gxt.data.shared.loader.LoaderHandler;

public class GridPanel<M> extends JPanel {

	private final JXTable table;
	private JComponent toolbar;
	private JComponent pagingToolbar;
	private JLoadComponent loadComponent;
	private boolean loadMask = true;
	private String loadMaskMsg = "加载中...";
	private final LoaderHandler<ListLoadConfig, ListLoadResult<M>> loadHandler = new LoaderHandler<ListLoadConfig, ListLoadResult<M>>() {

		@Override
		public void onBeforeLoad(final BeforeLoadEvent<ListLoadConfig> event) {
			SwingUtilities.invokeLater(() -> {
				columnModel.clear();
				if (isLoadMask()) {
					loadComponent.mask(loadMaskMsg);
				}
			});
		}

		@Override
		public void onLoad(LoadEvent<ListLoadConfig, ListLoadResult<M>> event) {
			List<M> data = event.getLoadResult().getData();
			SwingUtilities.invokeLater(() -> {
				columnModel.addAll(data);
				if (isLoadMask()) {
					loadComponent.unmask();
				}
			});
		}

		@Override
		public void onLoadException(LoadExceptionEvent<ListLoadConfig> event) {
			SwingUtilities.invokeLater(() -> {

				if (isLoadMask()) {
					loadComponent.unmask();
				}
			});
		}
	};
	private HandlerRegistration loaderRegistration;
	private ListLoader loader;
	private ColumnModel<M> columnModel;

	public GridPanel(ColumnModel<M> cm) {
		columnModel = cm;
		table = new JXTable();
		table.setModel(cm);
		table.setColumnControlVisible(true);
		table.setAutoCreateRowSorter(true);

		table.setRolloverEnabled(true);
		table.setShowGrid(false, false);
		table.addHighlighter(HighlighterFactory.createSimpleStriping());

		loadComponent = new JLoadComponent();
		loadComponent.setView(new JScrollPane(table));

		this.setLayout(new BorderLayout());
		add(loadComponent, BorderLayout.CENTER);
	}

	public void setLoader(ListLoader<?, ?> loader) {
		if (this.loaderRegistration != null) {
			loaderRegistration.removeHandler();
			loaderRegistration = null;
		}
		this.loader = loader;
		if (loader != null) {
			loaderRegistration = loader.addLoaderHandler((LoaderHandler) loadHandler);
		}
	}

	public boolean isLoadMask() {
		return loadMask;
	}

	public void setLoadMask(boolean loadMask) {
		this.loadMask = loadMask;
	}

	public JComponent getToolbar() {
		return toolbar;
	}

	public void setToolbar(JComponent tb) {
		if (tb != null) {
			// 删除旧的toolbar
			if (toolbar != null) {
				this.remove(toolbar);
			}
			this.toolbar = tb;
			add(toolbar, BorderLayout.NORTH);
		}
	}

	public JComponent getPagingToolbar() {
		return pagingToolbar;
	}

	public void setPagingToolbar(JComponent ptb) {
		if (ptb != null) {
			// 删除旧的pagingToolbar
			if (pagingToolbar != null) {
				this.remove(pagingToolbar);
			}
			this.pagingToolbar = ptb;
			add(pagingToolbar, BorderLayout.SOUTH);
		}
	}

	public String getLoadMaskMsg() {
		return loadComponent.getMaskMessage();
	}

	public void setLoadMaskMsg(String loadMaskMsg) {
		loadComponent.setMaskMessage(loadMaskMsg);
	}
}
